package com.css.masterslave.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.css.masterslave.mapper.MyUserMapper;
import com.css.masterslave.pojo.MyUser;
import com.css.masterslave.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MyUserServiceImpl implements MyUserService {
    @Autowired
    MyUserMapper myUserMapper;

    @Transactional
    @Override
    public int updateOptimstic(Long selectId,MyUser user) {
        MyUser myUser=myUserMapper.selectById(selectId);
        myUser.setAge(user.getAge());
        myUserMapper.updateById(myUser);
        return 0;
    }

    @Override
    public MyUser getUserById(Integer id) {

        return (MyUser) myUserMapper.selectById(id);
    }

    @Transactional
    @Override
    public int insertUser(MyUser myUser) {
        int res=myUserMapper.insert(myUser);
        return res;
    }

    @Override
    public List<MyUser> selectAllUsers() {
        return myUserMapper.selectList(null);
    }

    @Override
    public int updateMyUser(MyUser myUser) {
        int res = myUserMapper.updateById(myUser);
        return res;
    }

    @Override
    public Page<MyUser> selectPage(int cur, int size) {
        Page<MyUser> page=new Page<>(cur,size);
        Page<MyUser> myUserPage = myUserMapper.selectPage(page, null);
        return myUserPage;
    }

    @Override
    public int deleteMyUserById(Integer id) {
        int res = myUserMapper.deleteById(id);
        return res;
    }
}
